package org.sosie2.service.framework;

/**
 * 
 * The class wrapps an Exception object in order to deliver a customized exception. It holds all information needed 
 * to know about what kind of exception occured, and its cause.
 * @author Gabriela
 */
public class SosieException extends Exception
{
	private static final long serialVersionUID = 7010138641008560022L;
/*
 * the detailed message of the exception
 */
    private String detailMessage;
/*
 * the cause of the exception
 */
    private SosieException cause = null;

/*
 * the stack trace in a vector form
 */
    private String[] stackTrace;

    static transient boolean showRootStackOnly = true;

    private String exceptionClass = null;

/*
 * the parent exception
 */    
    private Throwable originalException = null;

    public SosieException()
    {
        super();
    }

    public SosieException(String message) {
        super(message);
    }
    
    /**
     * This method associates all properties of a SOSIE exception with those of a given Throwable object.
     * @param exception - the Throwable object being wrapped
     */
    public SosieException(Throwable exception)
    {
        if (exception == null) throw new IllegalArgumentException("The exception cannot be null");
        originalException = exception;
        exceptionClass = exception.getClass().getName();
        setDetailMessage(exception.getMessage());
        setStackTrace((showRootStackOnly ? null : getStackAsString(exception.getStackTrace())));
        if (exception.getCause() != null)
        {
            setCause(new SosieException(exception.getCause()));
        }
        else
        {
            setStackTrace(exception.getStackTrace());
        }
    }

    /**
     * Getter for the detailed message of the exception.
     * @return the String of the detailed message
     */
    public String getDetailMessage()
    {
        return detailMessage;
    }
/**
 * Setter for the detailed message.
 * @param detailMessage
 */
    public void setDetailMessage(String detailMessage)
    {
        this.detailMessage = detailMessage;
    }
/**
 * Getter for the detailed cause of the exception.
 * @return the String of the cause message
 */
    public SosieException getCause()
    {
        return cause;
    }
/**
 * Setter for the detailed cause of the exception.
 * @param the SOSIE exception wrapping the cause message
 */
    public void setCause(SosieException cause)
    {
        this.cause = cause;
    }
/**
 * Getter for the detailed exception stack trace.
 * @return the array of Strings as stack trace
 */
    public String[] getExceptionStackTrace()
    {
        return stackTrace;
    }
/**
 * Setter for the detailed exception stack trace.
 * @param the array of Strings or Strack trace elements as stack trace
 */
    public void setStackTrace(StackTraceElement[] stackTrace)
    {
        this.stackTrace = getStackAsString(stackTrace);
    }

    public void setStackTrace(String[] stackTrace)
    {
        this.stackTrace = stackTrace;
    }
/**
 * Getter for the exception class.
 * @return the String name of the class
 */
    public String getExceptionClass()
    {
        return exceptionClass;
    }

    public void setExceptionClass(String exceptionClass)
    {
        this.exceptionClass = exceptionClass;
    }

    protected String[] getStackAsString(java.lang.StackTraceElement[] elements)
    {
        String[] trace = new String[elements.length];
        for (int i = 0; i < elements.length; i++)
        {
            trace[i] = elements[i].toString();
        }
        return trace;
    }
}

